package com.java.io.byte_stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 字节流
 * 如果需要操作图片、视频数据，就会用到字节流
 */
public class FileStream {

    public static void readFile_3(String path, String fileName) throws IOException {
        FileInputStream fis = new FileInputStream(path + fileName);

//        int num = fis.available();
        byte[] buf = new byte[fis.available()]; //定义一个刚刚好的缓冲区。不用在循环了。
        fis.read(buf);
        System.out.println(new String(buf));
        fis.close();
    }


    public static void readFile_2(String path, String fileName) throws IOException {
        FileInputStream fis = new FileInputStream(path + fileName);
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = fis.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }
        fis.close();
    }


    public static void readFile_1(String path, String fileName) throws IOException {
        FileInputStream fis = new FileInputStream(path + fileName);
        int ch = 0;
        while ((ch = fis.read()) != -1) {
            System.out.println((char) ch);
        }
        fis.close();
    }


    public static void writeFile(String path, String fileName) throws IOException {
        FileOutputStream fos = new FileOutputStream(path + fileName);

        fos.write("abcde".getBytes());
        fos.close();
    }

}
